#include "port/datatype.h"
#include "onps_errors.h"
#include "port/sys_config.h"
#include "port/os_datatype.h"
#include "one_shot_timer.h"
#include "sys_timer.h"
#include "emac.h"
#include "protocols.h"

#if SUPPORT_PPP
#include "ppp/ppp.h"
#endif

#define SYMBOL_GLOBALS
#include "port/os_adapter.h"
#undef SYMBOL_GLOBALS

#if SUPPORT_ETHERNET
//* 系统存在几个网卡局添加几个网卡名称(ETHERNET_NUM宏定义的数量与名称数量应一致)，注意网卡名称不能重名
const CHAR *or_pszaEthName[ETHERNET_NUM] = {
    "eth0"
};
#endif

//* 用户自定义变量声明区
//* ===============================================================================================
#define THOSTIMERCOUNT_STK_SIZE	256
__align(8) OS_STK THOSTIMERCOUNT_STK[THOSTIMERCOUNT_STK_SIZE];

static CHAR l_bTHMtxCurPIP = MTX_PIP_START_PRIO; 
//* ===============================================================================================

//* 协议栈内部工作线程列表
const static STCB_PSTACKTHREAD lr_stcbaPStackThread[] = {
	{ thread_one_shot_timer_count, (void *)0, (OS_STK *)&THOSTIMERCOUNT_STK[THOSTIMERCOUNT_STK_SIZE - 1], THOSTIMERCOUNT_PRIO },
}; 

//* 当前线程休眠指定的秒数，参数unSecs指定要休眠的秒数
void os_sleep_secs(UINT unSecs)
{
	OSTimeDlyHMSM(0, 0, unSecs, 0); 
}

//* 当前线程休眠指定的毫秒数，单位：毫秒 
void os_sleep_ms(UINT unMSecs)
{
    OSTimeDlyHMSM(0, 0, 0, unMSecs); 
}

//* 获取系统启动以来已运行的秒数（从0开始）
UINT os_get_system_secs(void)
{
	return GetElapsedSecs(); 
}

void os_thread_onpstack_start(void *pvParam)
{
	pvParam = pvParam; //* 避免编译器警告
	
	//* 建立工作线程
	INT i; 
	for (i = 0; i < (INT)(sizeof(lr_stcbaPStackThread) / sizeof(STCB_PSTACKTHREAD)); i++)	
		OSTaskCreate(lr_stcbaPStackThread[i].pfunThread, lr_stcbaPStackThread[i].pvParam, lr_stcbaPStackThread[i].pstkTop, lr_stcbaPStackThread[i].ubPrio);
}

HMUTEX os_thread_mutex_init(void)
{
  OS_CPU_SR  cpu_sr = 0u;
	UCHAR ubErr; 
  CHAR bTHMtxCurPIP; 
	
	if(l_bTHMtxCurPIP <= 0) //* 如果预留的PIP用完了就无法再建立一个有效的互斥锁了
		return INVALID_HMUTEX;
  
  OS_ENTER_CRITICAL();
  bTHMtxCurPIP = l_bTHMtxCurPIP--; 
  OS_EXIT_CRITICAL();
  
	return OSMutexCreate(bTHMtxCurPIP, &ubErr); 
}

void os_thread_mutex_lock(HMUTEX hMutex)
{
	UCHAR ubErr; 
	OSMutexPend(hMutex, 0, &ubErr);
}

void os_thread_mutex_unlock(HMUTEX hMutex)
{
	OSMutexPost(hMutex);
}

void os_thread_mutex_uninit(HMUTEX hMutex)
{
	UCHAR ubErr;   
  OS_CPU_SR cpu_sr = 0;
  
  OS_ENTER_CRITICAL();
  l_bTHMtxCurPIP++; 
  OS_EXIT_CRITICAL();
  
	OSMutexDel(hMutex, OS_DEL_ALWAYS, &ubErr); 
}

HSEM os_thread_sem_init(UINT unInitVal, UINT unCount)
{
	unCount = unCount; //* 避免编译器警告	
	return OSSemCreate((USHORT)unInitVal);
}

void os_thread_sem_post(HSEM hSem)
{
	OSSemPost(hSem);
}

INT os_thread_sem_pend(HSEM hSem, INT nWaitSecs)
{
	UCHAR ubErr; 
	OSSemPend(hSem, (UINT)(nWaitSecs * OS_TICKS_PER_SEC), &ubErr);	
	if(OS_ERR_NONE == ubErr)
		return 0; 
  else if(OS_ERR_TIMEOUT == ubErr)
    return 1; 
	else
		return -1; 
}

void os_thread_sem_uninit(HSEM hSem)
{
	UCHAR ubErr; 
	OSSemDel(hSem, OS_DEL_ALWAYS, &ubErr); 
}

#if SUPPORT_PPP
HTTY os_open_tty(const CHAR *pszTTYName)
{
	pszTTYName = pszTTYName; //* 避免编译器警告
	return 0; 
}

void os_close_tty(HTTY hTTY)
{
	hTTY = hTTY; //* 避免编译器警告
}

INT os_tty_send(HTTY hTTY, UCHAR *pubData, INT nDataLen)
{
	hTTY = hTTY; //* 避免编译器警告
	return 0;
}

INT os_tty_recv(HTTY hTTY, UCHAR *pubRcvBuf, INT nRcvBufLen, INT nWaitSecs)
{
	hTTY = hTTY; //* 避免编译器警告
	return 0; 
}

void os_modem_reset(HTTY hTTY)
{
}
#endif

